<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        table
        {
            border-collapse:collapse;
        }

        table, td, th
        {
            border:1px solid black;
        }
    </style>
    <script src="scripts/jquery-1.8.3.js"></script>
    <script src="scripts/timejs.js"></script>
</head>
<body onload="maintabledata();" >

<!--<input type="button"  onclick="maintabledata();" value="查询"/>-->
<script type="text/javascript">
    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    var  itemcode;
    var heads = new Array();
    var Lines = new Array();
    var datanav = new Array();
    var backmonthup;
    var backmonthup=7;
    function unique(arr) {
        var result = [], hash = {};
        for (var i = 0, elem; (elem = arr[i]) != null; i++) {
            if (!hash[elem]) {
                result.push(elem);
                hash[elem] = true;
            }
        }
        return result;
    }

    itemcode=GetQueryString("id");
//    itemcode = 'B10012284';
    function maintabledata(){
        $.ajax({
            url: "/sap/query_item_detail.php",
            //url: "http://192.168.1.11:8090/dv/profitrate.php",
            type: "POST",
            dataType: 'jsonp',
            jsonp: 'itemcode',
            jsonpCallback: itemcode,
            timeout: 200000,
            success: function (jsonp) {//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
                console.log(jsonp);
                var datanav = jsonp;
                //console.log(datanav);
                var tablemain =document.getElementById("tb1");
                //console.log(rows);
                if ( tablemain.rows.length>1) {
                    for ( var i = tablemain.rows.length-1; i>0; i--)
                    {
                        tablemain.deleteRow (i);
                    }		}
                var tabletitle =document.getElementById("tb1title");
                if (tabletitle.cells.length>1) {
                    for ( var i = tabletitle.cells.length-1; i>0; i--)
                    {
                        tabletitle.deleteCell (i);
                    }		}

                for(var i=0 ; i<backmonthup;i++){
                    var ths=document.getElementById('tb1title').insertCell()
                    tablemain.insertCell;
                    m=DateAdd('m',-i,);
                    heads[i] = m.Format("yyMM")
                    ths.innerHTML=heads[i];
                }//画列头

                for ( var i = 0; i <datanav.length; i++)
                {
                    Lines[i]=datanav[i]['cardcode'];
                }
                Lines=unique(Lines);//行去重
                var dataformated = new Array();
                for ( var i = 0; i <Lines.length; i++)
                {
                    dataformated[i] = new Array();
                    var trs=document.getElementById('tb1').insertRow()
                    for(var irow=-1 ; irow<backmonthup;irow++)
                    {
                        trs.insertCell();
                    }
                }//画数据行再画数据列

                //填充数据
                for ( var i1 = 0; i1 <datanav.length; i1++)
                {
                    //alert(datanav[i1]['cardcode'])
                    for ( var i2 = 0; i2 <Lines.length+1; i2++)
                    {
                        if(datanav[i1]['cardcode']==Lines[i2])
                        {

                            for ( var i3 = 0; i3<heads.length; i3++)
                            {

                                if(datanav[i1]['ymonth']==heads[i3])
                                {
                                    //alert (datanav[i1]['ymonth']);
                                    //dataformated[i2][0]=datanav[i1]['cardcode']
                                    dataformated[i2][0]=datanav[i1]['cardname']
                                    dataformated[i2][i3+1]=datanav[i1]['profitrate']
                                }
                            }
                        }
                    }
                }

                for(var iRow =0;iRow<tablemain.rows.length-1;iRow++)
                {
                    for(var icell =0;icell<dataformated[iRow].length;icell++)
                    {
                        //alert(dataformated[iRow])
                        if (dataformated[iRow][icell]!=undefined)
                        {
                            document.getElementById('tb1').rows[iRow+1].cells[icell].innerHTML=dataformated[iRow][icell];
                        }
                    }
                }

                //填充数据结束


            }/*对jsonp返回函数的*/
        }/*对AJAX*/);


    }
</script>
<table id="tb1" >
    <tr id="tb1title">
        <th id='cardcode' name='maintitle'>平台</th>

    </tr>
</table>

</body>
</html>
